layui.use(['form', 'table', 'laydate'], function () {
    let table = layui.table;
    let laydate = layui.laydate;
    let form = layui.form;
    //日期
    laydate.render({
        elem: '#start',
        trigger: 'click'
    });
    //日期
    laydate.render({
        elem: '#end',
        trigger: 'click'
    });
    //页面加载完成要去执行findAll；
    findAll({});

    function findAll(data) {
        table.render({
            elem: '#test'
            , url: '/back/user/findAll'
            , where: data
            , cols: [[
                {field: 'id', width: 80, fixed: 'left', title: 'ID', sort: true}
                , {field: 'username', width: 80, title: '用户名'}
                , {field: 'sex', width: 50, title: '性别', sort: true}
                , {field: 'birthday', width: 180, title: '生日'}
                // , {
                //     field: 'enable', title: '状态', width: 80, templet: function (d) {
                //         return d.enable == 1 ? "<span style='color: green'>启用</span>" : "<span style='color: gray'>停用</span>";
                //     }
                // }
                , {field: 'enable', width: 80, title: '状态', templet: "<div>{{d.enable==1?'启用':'停用'}}</div>"}
                , {field: 'phone', width: 180, title: '电话', sort: true}
                , {field: 'address', width: 80, title: '地址', sort: true}
                , {field: 'hobby', width: 80, title: '爱好'}
                , {field: 'email', width: 185, title: '邮箱', sort: true}
                , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 150}
            ]]
            , page: true//开启分页
        });
    }

    //监听行工具事件
    table.on('tool(test)', function (obj) {
        var data = obj.data;
        console.log(obj)
        if (obj.event === 'del') {
            layer.confirm('真的删除行么', function (index) {
                let res = myAjax("/back/user/del", {id: data.id}, "post");
                console.log(res);
                if (res.count > 0) {
                    obj.del();
                    layer.close(index);
                } else {
                    layer.msg("删除失败");
                }

            });
        } else if (obj.event === 'edit') {
            // layer.prompt({
            //     formType: 2
            //     , value: data.email
            // }, function (value, index) {
            //     obj.update({
            //         email: value
            //     });
            //     layer.close(index);
            // });
            sessionStorage.setItem("userId", data.id);
            xadmin.open('添加用户', '/html/user/member-edit.html', 600, 400)
        }
    });
    form.on('submit(sreach)', function (data) {
        data = data.field;
        let arr_box = [];
        $('input[type=checkbox][name=hobby]:checked').each(function () {
            arr_box.push($(this).attr("title"));
        });
        //数组
        console.log(arr_box);
        data.hobby = arr_box.toLocaleString();
        console.log(data, 11)
        findAll(data);
        return false;
    })
});